﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Img 的摘要说明
/// </summary>
public partial class Img
{
    public enum ImageType { 电子产品 = 1, 论坛 };
    public static Img Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Img.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<Img> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Img.ToList();
        }
    }

    public static List<Img> GetAllByForeignID_TypeID(int ID, int TypeID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Img.Where(row => row.ForeignID == ID && row.Type == TypeID).ToList();
        }
    }
    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Img.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Img>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.Img.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.Img.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}